*This program generates a set of stochastic projections of the GDP gap, employment gap, and S&P 500

clear all
set more off
set maxvar 120000

* Filepath
local mypath "D:\Sloan-recessions-project"
cd "`mypath'"

use macro_data_4_2_19
*baseline is January 2017 CBO projection
drop new_pct_gdp_gap
*drop new_pct_employment_gap
	gen productivity_factor = 1 if year==2018
	
local i = 1
while `i' < 5001 {
	drawnorm random_error`i', sds(.0177593)
*This is the value from historical residuals
	
	
	gen new_pct_gdp_gap`i' = pct_gdp_gap if year < 2018
	replace new_pct_gdp_gap`i' = 0 if year==2018
	replace new_pct_gdp_gap`i' = .5997091*L1.new_pct_gdp_gap`i' + random_error`i' if year > 2018

	drawnorm empl_random_error`i', sds(.0051026)
	gen tot_empl_random_error`i' = 0 if year==2018
	replace tot_empl_random_error`i' = .7161374
*This is the value from historical residuals	
	gen new_pct_employment_gap`i' = .5825616*new_pct_gdp_gap`i' +  .3097003*L1.new_pct_gdp_gap`i' +  .0828865

	local prodfactor = rnormal(0,0.00820902)  
*based on sd of average decadal growth rate of real potential gdp from CBO potential gdp spreadsheet 51137-2019-01-potentialgdp

	replace productivity_factor = l1.productivity_factor*(`prodfactor'+1) if year>2018 
*new method with random draw of average productivity growth

	
	gen pct_change_employment`i' = (new_pct_employment_gap`i'+1
	gen pct_change_gdp`i' = (1+new_pct_gdp_gap`i

	drawnorm sp_random_error`i', sds(.1120767)
	gen tot_sp_random_error`i' = 0 if year==2018
	replace tot_sp_random_error`i' = .8943587
	gen new_sp500`i' =  exp(.0716511*year + 4.994365	gen pch_del_new_sp500`i' = (new_sp500`i' - l1.new_sp500`i')/l1.new_sp500`i'
	gen gdp`i' = nominal_gdp_baseline*(1+pct_change_gdp`i')
	replace gdp`i' = nominal_gdp_bea if year==2017	
	replace gdp`i' = nominal_gdp_baseline if year==2018	
	label variable gdp`i' "Nominal Gross Domestic Product"
	gen employment`i' = hh_employment_baseline*(1+pct_change_employment`i')
	replace employment`i' = hh_employment_cps/1000000 if year==2017
	replace employment`i' = hh_employment_baseline if year==2018
	local ++i
	}
outfile year gdp* employment* new_sp500* using stochastic_projections_2019_rlprod.csv if (year>2018 & year<2028), wide comma replace


save stochastic_projections_2019_rlprod.dta, replace

reshape long random_error empl_random_error sp_random_error tot_empl_random_error tot_sp_random_error gdp employment new_sp500 pch_del_new_sp500 new_pct_gdp_gap new_pct_employment_gap pct_change_gdp pct_change_employment, i(year) j(projection_number)

save stochastic_projections_2019_rlprod_long.dta, replace

